﻿<UserControl x:Class="AzureDiagMon.Views.Screens.LogViewerScreen"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
             xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
             xmlns:WPF="clr-namespace:AzureDiagMon.Common;assembly=AzureDiagMon.Common.WPF"
             xmlns:Helpers="clr-namespace:AzureDiagMon.Common.Helpers;assembly=AzureDiagMon.Common.WPF"
             xmlns:Screens="clr-namespace:AzureDiagMon.ViewModels.Screens;assembly=AzureDiagMon.ViewModels"
             mc:Ignorable="d"
             d:DesignHeight="479" d:DesignWidth="820"
             d:DataContext="{d:DesignInstance Type=Screens:LogViewerScreenViewModel, IsDesignTimeCreatable=True}">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/AzureDiagMon.Views.WPF;component/Themes/Generic.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <cmd:EventToCommand Command="{Binding LoadedEventCommand}" />
        </i:EventTrigger>
        <i:EventTrigger EventName="KeyDown">
            <cmd:EventToCommand Command="{Binding KeyDownEventCommand}" PassEventArgsToCommand="True"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*" />
            <ColumnDefinition Width="3" />
            <ColumnDefinition Width="1*" MaxWidth="200" />
        </Grid.ColumnDefinitions>
        <GridSplitter Grid.Column="1" Width="3" Margin="0"
                     HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ShowsPreview="True" />

        <Border Grid.Row="0" Grid.Column="0" Margin="3,2,0,3"
                BorderBrush="{StaticResource Flat3DBorderBrush}" BorderThickness="1">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="26" />
                    <RowDefinition Height="*" />
                    <!--<RowDefinition Height="100" />-->
                </Grid.RowDefinitions>
                <Grid Background="#696969">
                    <!--<Label Content="{Binding Title}"/>-->
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <StackPanel Orientation="Horizontal" Grid.ColumnSpan="2">
                            <Label Foreground="White" FontWeight="Bold">Trace Logs</Label>
                            <Label Foreground="White">Number of entries:</Label>
                            <Label Foreground="White" Content="{Binding NumberOfEntries, FallbackValue=0}" />
                            <Label Foreground="White" Content="{Binding Message}" />
                        </StackPanel>
                        <Label Grid.Column="1" Foreground="White" Content="{Binding TimeModeLabel}" ></Label>
                    </Grid>
                </Grid>
                <!--<ToolBar Grid.Row="0">
        </ToolBar>-->
                <Grid Grid.Row="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="3" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>


                    <!--Event list grid-->
                    <!--<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding LogEntries}">
            </DataGrid>-->
                    <ProgressBar Grid.Row="0" Style="{StaticResource BorderlessProgressBar}"
                                 Maximum="{Binding RefreshInterval}"
                                 IsIndeterminate="True"                                 
                                 Visibility="{Binding Main.LogSyncService.IsRefreshing, Converter={StaticResource VisibilityConverter}, ConverterParameter=True}" />
                    <ListView Grid.Row="1" ItemsSource="{Binding LogEntries}" 
                              x:Name="LogEntriesListView"
                              Helpers:ListViewExtensions.Stretch="true"                              
                              SelectedItem="{Binding SelectedLogEntry}"
                              SelectionMode="Extended">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="SelectionChanged">
                                <cmd:EventToCommand Command="{Binding SelectionChangedCommand}"
                                                    CommandParameter="{Binding SelectedItems,
                                                    ElementName=LogEntriesListView}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Level" Width="80">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="20" />
                                                    <ColumnDefinition Width="*" />
                                                </Grid.ColumnDefinitions>
                                                <Image Source="{Binding Level, Converter={StaticResource WadLogLevelToIconImageConverter}}"
                                                       Width="16" Height="16"
                                                       Margin="0,1,4,0" />
                                                <TextBlock Grid.Column="1" 
                                                           Text="{Binding Level, Converter={StaticResource WadLogLevelToStringConverter}}"
                                                           />
                                            </Grid>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn DisplayMemberBinding="{Binding Path=RoleInstance}" 
                                                Header="Role Instance" Width="150" />
                                <GridViewColumn DisplayMemberBinding="{Binding Path=EventTime}" 
                                                Header="Date and Time" Width="120">
                                </GridViewColumn>
                                <!--<GridViewColumn DisplayMemberBinding="{Binding Path=EventId}" 
                                                Header="Event ID" Width="60" />-->
                                <GridViewColumn DisplayMemberBinding="{Binding Path=Message}" 
                                                Header="Message" />
                            </GridView>
                        </ListView.View>
                    </ListView>
                    
                    <GridSplitter Grid.Row="2" Height="3" Margin="0"
                                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                                  ShowsPreview="True" />

                    <!--Event details-->
                    <Grid Grid.Row="3" Margin="5" DataContext="{Binding SelectedLogEntry}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0" Margin="3">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="23" />
                                <ColumnDefinition Width="579" />
                            </Grid.ColumnDefinitions>
                            <Image Source="{Binding Level, Converter={StaticResource WadLogLevelToIconImageConverter}}"
                                                       Width="16" Height="16"
                                                       Margin="0,1,4,0" />
                            <TextBlock Grid.Column="1" Text="{Binding Message}" TextTrimming="CharacterEllipsis" />
                        </Grid>

                        <Grid Grid.Row="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="80" />
                                <ColumnDefinition MinWidth="150" MaxWidth="250" />
                                <ColumnDefinition Width="80" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

                            <TextBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Margin="4"
                                     TextWrapping="Wrap" AcceptsReturn="True"
                                     ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                                     ScrollViewer.VerticalScrollBarVisibility="Auto"
                                     Text="{Binding Message, Mode=OneWay}"
                                     IsReadOnly="True"/>
                            
                            <Label Grid.Row="1" Grid.Column="0" Content="Role:" />
                            <TextBox Grid.Row="1" Grid.Column="1" Style="{StaticResource LabelTextBox}"
                                     Text="{Binding Role, Mode=OneWay}" />
                            <Label Grid.Row="1" Grid.Column="2" 
                                   Content="Instance:" />
                            <TextBox Grid.Row="1" Grid.Column="3" Style="{StaticResource LabelTextBox}"
                                     Text="{Binding RoleInstance, Mode=OneWay}"/>

                            <Label Grid.Row="2" Grid.Column="0" Content="Logged:" />
                            <TextBox Grid.Row="2" Grid.Column="1" Style="{StaticResource LabelTextBox}"
                                     Text="{Binding Path=EventTime, Mode=OneWay, StringFormat=\{0:d\} \{0:HH:mm:ss.fff\}}"/>
                            <Label Grid.Row="2" Grid.Column="2" Content="Transferred:" />
                            <TextBox Grid.Row="2" Grid.Column="3" Style="{StaticResource LabelTextBox}"
                                     Text="{Binding Path=Timestamp, Mode=OneWay, StringFormat=\{0:d\} \{0:HH:mm:ss.fff\}}"/>

                            <Label Grid.Row="3" Grid.Column="0" Content="Event ID:" />
                            <TextBox Grid.Row="3" Grid.Column="1" Style="{StaticResource LabelTextBox}"
                                     Text="{Binding Path=EventId, Mode=OneWay}" />
                            <Label Grid.Row="3" Grid.Column="2" Content="Level:" />
                            <Grid Grid.Row="3" Grid.Column="3">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="20" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Image Source="{Binding Level, Converter={StaticResource WadLogLevelToIconImageConverter}}"
                                                       Width="16" Height="16"
                                                       Margin="0,1,4,0" />
                                <TextBox Grid.Column="1" Style="{StaticResource LabelTextBox}"
                                         Text="{Binding Level, Mode=OneWay, Converter={StaticResource WadLogLevelToStringConverter}}" />
                            </Grid>


                        </Grid>
                    </Grid>                   
                </Grid>
            </Grid>
        </Border>
        <Border Grid.Column="2" Margin="0,2,3,3" 
                BorderBrush="{StaticResource Flat3DBorderBrush}" BorderThickness="1" >
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="26" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Label Content="Actions" FontWeight="Bold" Background="#eee" />
                <Expander Grid.Row="1" Header="Operational" IsExpanded="True" ExpandDirection="Down"
                          Style="{StaticResource CommandsExpander}">
                    <StackPanel Margin="24,3,0,0">
                        <Button Content="Refresh"
                                Style="{StaticResource CommandButton}" 
                                Command="{Binding RefreshCommand}" Height="23" />
                        <Button Content="{Binding ToggleAutosyncButtonTitle}"
                                Style="{StaticResource CommandButton}" 
                                Command="{Binding ToggleAutosyncCommand}" Height="23" />
                        <StackPanel Orientation="Horizontal" Visibility="{Binding Main.LogSyncService.IsEnabled, Converter={StaticResource VisibilityConverter}, ConverterParameter=True}">
                            <Label Content="Interval: " />
                            <xctk:IntegerUpDown Grid.Column="1" MaxWidth="60"
                                                Value="{Binding RefreshInterval}"
                                                Increment="10" />
                            <Label Grid.Column="2" Margin="3,0,0,0" Content="secs" />
                        </StackPanel>
                    </StackPanel>
                </Expander>
                <Expander Grid.Row="2" Header="Current View" IsExpanded="True">
                    <StackPanel Margin="24,1,3,0">
                        <Button 
                            Content="{Binding ToggleLocalTimeButtonTitle}"
                            Style="{StaticResource CommandButton}" 
                            Command="{Binding ToggleLocalTimeCommand}" Height="23" />

                        <GroupBox Header="Date filter">
                            <StackPanel Margin="3">
                                <ComboBox Margin="3"
                                          ItemsSource="{Binding DateFilters}"
                                          SelectedItem="{Binding SelectedDateFilter}"/>
                                <Grid Visibility="{Binding SelectedDateFilter.IsCustom, Converter={StaticResource VisibilityConverter}, ConverterParameter=True}">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <DatePicker Grid.Row="0" Margin="3"                                        
                                        SelectedDate="{Binding FilterStartDate}" />
                                    <DatePicker Grid.Row="1" Margin="3"
                                        SelectedDate="{Binding FilterEndDate}" />
                                </Grid>
                            </StackPanel>
                        </GroupBox>

                        <GroupBox Header="Level">
                            <WPF:CheckableItemsWrapPanel Margin="3"
                                                         ItemsSource="{Binding TraceLogLevels}"
                                                         CheckedItems="{Binding SelectedTraceLogLevels, Mode=TwoWay}"
                                                         PresentationTraceSources.TraceLevel="High">
                                <!--<WPF:CheckableItemsWrapPanel.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="20" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>

                                            <Image Source="{Binding Converter={StaticResource TraceLevelToIconImageConverter}}"
                                                   Width="16" Height="16"
                                                   Margin="0,1,4,0" />
                                            <TextBlock Grid.Column="1" Text="{Binding Converter={StaticResource LogLevelToStringConverter}}" />
                                        </Grid>
                                    </DataTemplate>
                                </WPF:CheckableItemsWrapPanel.ItemTemplate>-->
                            </WPF:CheckableItemsWrapPanel>

                            <!--<xctk:CheckListBox ItemsSource="{Binding Source={StaticResource LogLevels}}"
                                               SelectedValue="{Binding SelectedLevels}"
                                                >
                                <xctk:CheckListBox.ItemTemplate>
                                </xctk:CheckListBox.ItemTemplate>
                            </xctk:CheckListBox>-->
                        </GroupBox>
                    </StackPanel>

                </Expander>
            </Grid>
        </Border>
        <Border Grid.Row="1" Grid.ColumnSpan="3" 
                BorderThickness="0,1,0,0" BorderBrush="Gray">
            <StatusBar Height="22"
                   Background="{StaticResource StatusBarBrush}"
                   FontSize="11">
                <Border Visibility="{Binding Main.LogSyncService.IsRefreshing, Converter={StaticResource VisibilityConverter}}">
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock Text="Idle." Margin="3,0,3,0" Visibility="{Binding Main.LogSyncService.IsEnabled, Converter={StaticResource VisibilityConverter}, ConverterParameter=True}" />

                        <TextBlock Text="Next refresh in " Margin="3,0,0,0" Visibility="{Binding Main.LogSyncService.IsEnabled, Converter={StaticResource VisibilityConverter}, ConverterParameter=True}"/>
                        <TextBlock Text="{Binding Main.LogSyncService.RemainingTimeDisplayText}" Visibility="{Binding Main.LogSyncService.IsEnabled, Converter={StaticResource VisibilityConverter}, ConverterParameter=True}"/>
                    </StackPanel>
                </Border>
                <TextBlock Text="Refreshing" Margin="3,0,0,0" Visibility="{Binding Main.LogSyncService.IsRefreshing, Converter={StaticResource VisibilityConverter}, ConverterParameter=True}" />
                <Border BorderThickness="0" Width="14" Height="14">
                    <Border Visibility="{Binding Main.LogSyncService.IsRefreshing, Converter={StaticResource VisibilityConverter}, ConverterParameter=True}">
                        <Control Template="{StaticResource LoadingAnimation}" />
                    </Border>
                </Border>
            </StatusBar>
        </Border>
    </Grid>
</UserControl>
